package com.zxh.okretrofit.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

import static android.os.Build.VERSION.SDK_INT;

/**
 * 网络相关工具类
 * Created by zhangxuehui on 2018/2/8.
 */
public class NetUtils {
    private NetUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 获取网络状态,4G、WIFI、未联网
     *
     * @param context
     * @return 未联网：-1，4G：0，WIFI：1,其他：100
     */
    public static int getNetworkState(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null != connectivityManager) {
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            if (null != info && info.isConnected()) {
                if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                    return ConnectivityManager.TYPE_WIFI;
                } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                    return ConnectivityManager.TYPE_MOBILE;
                } else {
                    return 100;
                }
            }
        }
        return -1;
    }

    /**
     * 是否联网
     *
     * @param context
     * @return true 已经联网
     */
    public static boolean isConnected(Context context) {
        /*获取网络链接活动管理器*/
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null != connectivityManager) {
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            if (null != info && info.isConnected()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否连接WIFI
     *
     * @param context
     * @return true 连接WIFI
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        }
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }
        return info.getType() == ConnectivityManager.TYPE_WIFI;
    }

    /**
     * 是否连接移动网络
     *
     * @param context
     * @return true 连接移动网络
     */
    public static boolean isMobile(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        }
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }
        return info.getType() == ConnectivityManager.TYPE_MOBILE;
    }

    /**
     * 打开设置页面
     *
     * @param context
     */
    public static void openSetting(Context context) {
        Intent intent;
        if (SDK_INT > 10) {
            intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        } else {
            intent = new Intent();
            ComponentName component = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
            intent.setComponent(component);
            intent.setAction("android.intent.action.VIEW");
        }
        context.startActivity(intent);
    }
}
